using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public abstract class RespawnHandler : MonoBehaviour {
	
	private List<GameObject> respawnPoints = new List<GameObject>();
	private int Speed;
	
	public int addPoint(GameObject gameobject){
		int index = respawnPoints.Count;
		respawnPoints.Add(gameobject);
		return index;
	}
	
	public GameObject getPoint(int spawnIndex) {
		return respawnPoints[spawnIndex];
	}
	
	public virtual bool canSpawn(int spawnIndex) {
		return true;
	}
	
	public List<GameObject> getRespawnPoints() {
		List<GameObject> clone = new List<GameObject>();
		
		for (int i = 0; i < respawnPoints.Count; i++) {
			clone.Insert(Random.Range (0, clone.Count), respawnPoints[i]);
		}
		
		return clone;
	}
	
	public virtual bool spawnObject(GameObject gameObject) {
		List<GameObject> randomList = getRespawnPoints ();
		
		for (int i = 0; i < randomList.Count; i++) {
			if (canSpawn(i)) {
				spawnObject(gameObject, i);
				return true;
			}
		}
		
		return false;
	}
	
	protected void spawnObject(GameObject gameobject, int respawnPointIndex){
		Vector3 spawnPos = respawnPoints[respawnPointIndex].transform.position;
		gameobject.transform.position = spawnPos;
	}
	
}